/**
 * Project Name:spring-cloud-pilot File Name:CommandUtil.java Package Name:org.spring.cloud.pilot.util
 * Date:2019年10月24日下午5:36:10 Copyright (c) 2019, dengqian37@126.com All Rights Reserved.
 *
 */

package org.spring.cloud.pilot.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ClassName:CommandUtil <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2019年10月24日 下午5:36:10 <br/>
 * 
 * @author dengqian
 * @version
 * @since JDK 1.8
 * @see
 */
public class CommandUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommandUtil.class);

    public static void commandlinux(String sh) {
        try {
            Process process = Runtime.getRuntime().exec(new String[] {"/bin/sh", "-c", sh});

            printMessage(process.getInputStream());
            printMessage(process.getErrorStream());
            int value;

            value = process.waitFor();
            LOGGER.info(String.valueOf(value));

        } catch (InterruptedException | IOException e) {

            LOGGER.error("CommandUtil", e);

        }
    }
    
    
    public static void commandWindow(String sh) {
        try {
            System.out.println("Windows");

            // exeCmd = "cmd.exe /C start startZuulServer.bat";

            final Process process = Runtime.getRuntime()
                .exec(new String[] {"cmd", "/c", "java -jar spring-config-client-0.0.1.jar"});

            printMessage(process.getInputStream());
            printMessage(process.getErrorStream());
            int value = process.waitFor();
            System.out.println(value);

        } catch (InterruptedException | IOException e) {

            LOGGER.error("CommandUtil", e);

        }
    }

    private static void printMessage(final InputStream input) {
        new Thread(new Runnable() {
            public void run() {
                //
                Reader reader = new InputStreamReader(input);
                BufferedReader bf = new BufferedReader(reader);
                String line = null;
                try {
                    while ((line = bf.readLine()) != null) {
                        System.out.println(line);
                        LOGGER.info(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
